﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Newtonsoft.Json;

public partial class Bk_ZLPh : Basic
{
    public string strList = "";
    public string strPage = " ";
    public string strLoc = "流入板块";
    protected void Page_Load(object sender, EventArgs e)
    {
        strList = sbList().ToString();

        int type = OTO.ToInt(Request["t"], 1);
        if (type == 2)
            strLoc = "流出板块";

    }

    protected StringBuilder sbList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            int type = OTO.ToInt(Request["t"], 1);
            if (type < 1 || type > 3)
                type = 1;
            string str = Pub.LoadData(jsonUrl + "hangqing/bkph.ashx?count=100");
            BkphEnty obj = JsonConvert.DeserializeObject<BkphEnty>(str);
            if (obj != null)
            {
                string d = OTO.ToStr(Request["d"], "");
                string d2 = OTO.ToStr(Request["d2"], "");
                string t = type.ToString();
                string o = OTO.ToStr(Request["o"], "1");
                string o2 = OTO.ToStr(Request["o2"], "1");
                if (string.IsNullOrEmpty(d) && string.IsNullOrEmpty(d2))
                    d = "1";

                List<BkphItEnty> list = new List<BkphItEnty>();
                list = obj.行业板块;
                int bigNC = 1;
                switch (type)
                {
                    case 2:
                        bigNC = list.Max(x => Math.Abs(x.NC));
                        list = list.Where(x => x.J < 0).ToList();
                        break;
                    default:
                        bigNC = list.Max(x => Math.Abs(x.NC));
                        list = list.Where(x => x.J >= 0).ToList();
                        break;
                }

                if (d.Equals("1"))
                {
                    if (o.Equals("1"))
                        list = list.OrderByDescending(x => x.F).ToList();
                    else
                        list = list.OrderBy(x => x.F).ToList();
                }
                if (d2.Equals("1"))
                {
                    if (o2.Equals("1"))
                        list = list.OrderByDescending(x => x.F5).ToList();
                    else
                        list = list.OrderBy(x => x.F5).ToList();
                }

                o = o == "1" ? "0" : "1";
                o2 = o2 == "1" ? "0" : "1";

                sb.AppendLine("<div class=\"hqzx_cpxh_lt hqzx_cpxh_tborder\">");
                sb.AppendLine("<div class=\"hqzx_cpxh_m_2_nav\">");
                sb.AppendLine("<div class=\"index_content_h h3 gray_t hqzx_cpxh_m_t_l\"><img src=\"images/index_ico2.gif\" class=\"img\" />板块主力排行  </div>");
                sb.AppendLine("<ul>");

                if (type == 1)
                    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">流入板块</li>");
                else
                    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_ZLPh.aspx?t=1\">流入板块</a></li>");
                if (type == 2)
                    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">流出板块</li>");
                else
                    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_ZLPh.aspx?t=2\">流出板块</a></li>");

                sb.AppendLine("</ul>");
                sb.AppendLine("<div class=\"r_time gray\">&nbsp;</div>");
                sb.AppendLine("</div>");
                sb.AppendLine("<div class=\"tableLarge\">");

                sb.AppendLine("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" id=\"mainBtable\">");
                sb.AppendLine("<thead>");
                sb.AppendLine("<tr class=\"tableLargeHead\">");
                sb.AppendLine("<td>排序</td>");
                sb.AppendLine("<td>板块名称</td>");
                sb.AppendFormat("<td><a href=\"Bk_ZLPh.aspx?d=1&t={0}&o={1}\">涨跌幅{2}</a></td>", t, o, ud(d, o));
                sb.AppendFormat("<td><a href=\"Bk_ZLPh.aspx?d2=1&t={0}&o2={1}\">５日涨跌{2}</a></td>", t, o2, ud(d2, o2));
                //sb.AppendLine("<td>资金</td>");
                sb.AppendLine("<td>股票个数</td>");
                sb.AppendLine("<td>涨跌家数</td>");
                sb.AppendLine("<td>领涨股</td>");
                sb.AppendLine("<td>弱势股</td>");
                sb.AppendLine("<td width=\"260\">出B点天数统计</td>");
                sb.AppendLine("</tr>");
                sb.AppendLine("</thead>");
                sb.AppendLine("<tbody> ");

                int _totalCount = 0;
                int _pagesize = 30;
                int _totalPage = 0;
                _totalCount = list.Count;
                if (_totalCount > 0)
                {
                    if ((_totalCount % _pagesize) == 0)
                        _totalPage = _totalCount / _pagesize;
                    else
                        _totalPage = (int)(_totalCount / _pagesize) + 1;
                }
                int page = OTO.ToInt(Request["page"], 1);
                if (page == 0)
                    page = 1;
                if (page > _totalPage)
                    page = _totalPage;
                int _index = (page - 1) * _pagesize;
                int _end = page * _pagesize;
                if (page == _totalPage && (_totalCount % _pagesize) != 0)
                    _end = (page - 1) * _pagesize + _totalCount % _pagesize;

                for (int i = _index; i < _end; i++)
                {
                    sb.AppendFormat("<tr>");
                    sb.AppendFormat("<td>{0}</td>", i + 1);
                    sb.AppendFormat("<td><a href=\"bk{1}.shtml\" class=\"b\">{0}</a></td>", list[i].BKName, list[i].BKCode);
                    sb.AppendFormat("<td><span ph=\"zdf\">{0}</span></td>", OTO.PerRedGre(list[i].F, 2, "-", "%"));
                    sb.AppendFormat("<td><span ph=\"zdf5\">{0}</span></td>", OTO.PerRedGre(list[i].F5, 2, "-", "%"));
                    //sb.AppendFormat("<td>{0}</td>", list[i].J);
                    sb.AppendFormat("<td>{0}</td>", list[i].StockCount);
                    sb.AppendFormat("<td><span class=\"red\">{0}</span>/<span class=\"green\">{1}</span></td>", list[i].UpCount, list[i].DownCount);
                    sb.AppendFormat("<td><a href=\"gg{1}.shtml\" class=\"b\">{0}</a></td>", list[i].LZName, list[i].LZCode);
                    sb.AppendFormat("<td><a href=\"gg{1}.shtml\" class=\"b\">{0}</a></td>", list[i].LDName, list[i].LDCode);
                    //sb.AppendFormat("<td class=\"{1}\">{0}</td>", Math.Abs(list[i].NC), list[i].NC < 0 ? "bg_tipgreen" : "bg_tipred");
                    sb.AppendFormat("<td>");
                    sb.AppendFormat("{0}", PLine(bigNC, list[i].NC));
                    sb.AppendFormat("</td>");
                    sb.AppendFormat("</tr>");
                }
                sb.AppendLine("</tbody>");
                sb.AppendLine("</table>");

                if (_totalPage > 1)
                    strPage = "<div class=\"news_next\">" + PageHelper.fy(_totalCount, _pagesize, page, Request.Url.AbsoluteUri) + "</div>";

                sb.AppendLine("</div>");
                sb.AppendLine("</div>");


            }
        }
        catch
        { }
        return sb;
    }

    protected string PLine(int tol, int n)
    {
        StringBuilder sb = new StringBuilder();
        if (n != 0)
            sb.AppendFormat("<div class=\"nc_div\"><div class=\"nc_l\"></div><div class=\"nc_m\" style=\"width:{0}px;\">&nbsp;</div><div class=\"nc_r\"></div><div class=\"nc_s\">&nbsp;{1}</div></div>", int.Parse((220 * Math.Abs(n) / tol).ToString()), Math.Abs(n));
        else
            sb.AppendFormat("&nbsp;{0}", n);
        return sb.ToString();
    }

    protected string ud(string d, string o)
    {
        string rel = "";
        if (d == "1")
        {
            if (o == "1")
                rel = "<span class=\"green\">↓</span>";
            else if (o == "0")
                rel = "<span class=\"red\">↑</span>";
            else
                rel = "";
        }
        return rel;
    }
}